const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/sys_auth');
const SysAuthService = appRequire('service/desktop/SysAuthService.js');
const SysAuthTypeService = appRequire('service/desktop/SysAuthTypeService.js');
const ResultVo = require(appConfig.rootUrl+'/vo/ResultVo.js');

router.post('/save',async(ctx)=>{
  try{
    let auth = ctx.request.body;
    auth = await SysAuthService.save(auth);
    console.log(auth);
    ctx.body = ResultVo.success('success',auth);
  }catch(err){
    //集中处理错误
    err.msg = "新增授权失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let auth = ctx.query;
    auth = await SysAuthService.deleteOne(auth.id);
    ctx.body = ResultVo.success('success',auth);
  }catch(err){
    //集中处理错误
    err.msg = "删除授权失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await SysAuthService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "删除授权失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let auth = ctx.query;
    auth = await SysAuthService.findOne(auth.id);
    let authTypes = await SysAuthTypeService.findAllByType({type:'authType'});
    auth.authTypes = authTypes;
    console.log(auth);
    ctx.body = ResultVo.success('success',auth);
  }catch(err){
    //集中处理错误
    err.msg = "查找授权失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await SysAuthService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部授权数据失败!');
  }
});
router.get('/findAllUserAuths',async(ctx)=>{
  try{
    let data = await SysAuthService.findAllUserAuths();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取用户全部授权数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await SysAuthService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页授权数据失败!');
  }
});
module.exports = router;